package com.eightywork.life.adapter;

import java.util.List;

import com.data.carrier.u;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.eightywork.life.R;
import com.eightywork.life.activity.BaseActivity;
import com.eightywork.life.activity.IndexActivity;
import com.eightywork.life.activity.PmDetailActivity;
import com.eightywork.life.activity.SpaceActivity;
import com.eightywork.life.activity.StartActivity;
import com.eightywork.life.fragment.SpaceFragment;
import com.eightywork.life.imageCache.ImageLoader;
import com.eightywork.life.model.Space;
import com.eightywork.life.service.CpService;
import com.eightywork.life.util.AndroidUtil;
import com.eightywork.life.util.MyHttpException;

public class NeighborListAdapter extends ArrayAdapter<Space> {
	private LayoutInflater mInflater;
	private ImageLoader imageLoader;
	private Context n_context;
    private int mResource = R.layout.item_neighbor;
	public NeighborListAdapter(Context context, List<Space> list) {
		super(context, 0, list);
		n_context = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);
	}

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		View layout = convertView;
		if (layout == null) {
			layout = mInflater.inflate(mResource, parent, false);
		}
		final Space space = getItem(position);
		layout.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
					Intent intent = new Intent(getContext(), SpaceActivity.class);
					intent.putExtra("uid", space.getUid());
					getContext().startActivity(intent);
			}
		});
		TextView username = (TextView) layout.findViewById(R.id.username);
		username.setText(space.getName());
		ImageView avatar = (ImageView) layout.findViewById(R.id.avatar);
		imageLoader.DisplayImage(space.getAvatar(), avatar, 0, 0, 0);

		ImageButton mobile = (ImageButton) layout
				.findViewById(R.id.button_moblie);
		mobile.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
						+ space.getMobile()));
				getContext().startActivity(intent);
			}
		});
		ImageButton add = (ImageButton) layout.findViewById(R.id.button_add);
		RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)mobile.getLayoutParams();
		if (space.getVerifyType().equals("4")) {
			add.setVisibility(View.GONE);
			username.setPadding(0, 0, mobile.getWidth() + username.getPaddingRight(), 0);
			
			params.setMargins(0, 0, AndroidUtil.dpToPx(10, n_context), 0);
			mobile.setLayoutParams(params);
		} else {
			username.setPadding(0, 0, username.getPaddingRight() - mobile.getWidth(), 0);
			params.setMargins(0, 0, AndroidUtil.dpToPx(54, n_context), 0);
			mobile.setLayoutParams(params);
			add.setVisibility(View.VISIBLE);
			if (space.getIsmember() == 1) {
				add.setImageResource(R.drawable.neighbor_mute_button);
			} else {
				add.setImageResource(R.drawable.neighbor_press_button);
			}
			add.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (space.getIsmember() == 0) {
						new AddTask().execute(space.getUid());
					} else {
						new IgnoreTask().execute(space.getUid());
					}
				}
			});
		}

		return layout;
	}


	class AddTask extends AsyncTask<String, Void, String> {
		@Override
		protected void onPreExecute() {
			((BaseActivity) getContext()).showProgressDialog("请稍后...");
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... prames) {
			try {
				return CpService.friendAdd("", prames[0], "");
			} catch (MyHttpException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			((BaseActivity) getContext()).hideProgressDialog();
			if (result != null) {
				((BaseActivity) getContext()).showMessage(result);
			}
			super.onPostExecute(result);
		}
	}

	class IgnoreTask extends AsyncTask<String, Void, String> {
		@Override
		protected void onPreExecute() {
			((BaseActivity) getContext()).showProgressDialog("请稍后...");
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... prames) {
			try {
				return CpService.friendIgnore(prames[0]);
			} catch (MyHttpException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			((BaseActivity) getContext()).hideProgressDialog();
			if (result != null) {
				((BaseActivity) getContext()).showMessage(result);
			}
			super.onPostExecute(result);
		}
	}
}
